package clm.tail;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogInputThread extends Thread {
	String testString = "";
	int fileRef = 0;
	private static final Pattern DATE_PATTERN_DEFAULT = Pattern.compile("^\\s*(\\[\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d{3}\\]).*$");
	private static final Pattern DATE_PATTERN_SYSTEM = Pattern.compile("^\\s*(\\[\\d{1,2}/\\d{1,2}/\\d{2} \\d{1,2}:\\d{2}:\\d{2}:\\d{3} \\w{3}\\]).*$");
	private static final Pattern DATE_PATTERN_FILENET = Pattern.compile("^\\s*(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}).*$");
	private static final SimpleDateFormat SDF_DEFAULT = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss.SSS]");
	private static final SimpleDateFormat SDF_SYSTEM = new SimpleDateFormat("[M/d/yy HH:mm:ss:SSS z]");
	private static final SimpleDateFormat SDF_FILENET = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
	
	@Override
	public void run() {
		int matchNum = -1;
		int patternCount = 3;
		Pattern[] patterns = new Pattern[patternCount];
		patterns[0] = DATE_PATTERN_DEFAULT;
		patterns[1] = DATE_PATTERN_SYSTEM;
		patterns[2] = DATE_PATTERN_FILENET;
		SimpleDateFormat[] dateFormats = new SimpleDateFormat[patternCount];
		dateFormats[0] = SDF_DEFAULT;
		dateFormats[1] = SDF_SYSTEM;
		dateFormats[2] = SDF_FILENET;
		System.out.println(SDF_DEFAULT.format(new Date()) + " - Offer thread started - " + testString);
		int count = 1;
		BufferedReader in = null;
		boolean initialRun = true;
		try {
			in = new BufferedReader(new FileReader(testString));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
			return;
		}
		StringBuffer currentBuffer = new StringBuffer();
		boolean theBool = true;
		while(theBool){			
			currentBuffer = new StringBuffer();
			LogEntryDTO lineDTO = null;
			boolean firstLine = true;
			Matcher matcher;
	        String str;
	        try {
				while ((str = in.readLine()) != null) {
					if(!initialRun){
						matchNum = -1;
						for(int i = 0; i < patternCount; i++){
							matcher = patterns[i].matcher(str);
							if(matcher.find()){
								matchNum = i;
								if(firstLine){
									lineDTO = new LogEntryDTO();
									lineDTO.setLineDate(dateFormats[i].parse(matcher.group(1)));
									firstLine = false;
								}
								else{
									lineDTO.setLineText(currentBuffer.toString());
									lineDTO.setReadOrder(count++);
									lineDTO.setFileReference(fileRef);
									GlobalQueues.linePriorityQueue.put(lineDTO);
									lineDTO = new LogEntryDTO();
									lineDTO.setLineDate(dateFormats[i].parse(matcher.group(1)));
									currentBuffer = new StringBuffer();
								}
								currentBuffer.append(str);
								continue;
							}
						}
						if(matchNum == -1){
							currentBuffer.append("\n" + str);
						}
						
					}
				}
				if(initialRun){
					initialRun = false;
				}
					
				if(lineDTO != null){
					lineDTO.setLineText(currentBuffer.toString());
					lineDTO.setReadOrder(count++);
					lineDTO.setFileReference(fileRef);
					GlobalQueues.linePriorityQueue.put(lineDTO);
				}
	        } catch (IOException e1) {
				e1.printStackTrace();
				return;
			} catch (ParseException e) {
				e.printStackTrace();
				return;
			}
	        

			try {
				Thread.sleep(250L);
			} catch (InterruptedException e) {
				e.printStackTrace();
				return;
			}
		}
		try {
			in.close();
		} catch (IOException e1) {
			e1.printStackTrace();
			return;
		}
		System.out.println(SDF_DEFAULT.format(new Date()) + " - Offer thread finished.");
	}

	public synchronized void start(String test, int ref) {
		testString = test;
		fileRef = ref;
		super.start();
	}

}
